﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.Customization;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DTO;
using BUS;

namespace GUI
{
    public partial class GhiNhanDatVe_GUI : DevExpress.XtraEditors.XtraUserControl
    {
        public GhiNhanDatVe_GUI()
        {
            InitializeComponent();
        }
        
        int flag = 0;
        BindingList<GhiNhanDatVe_DTO> listGhiNhanDatVe = GhiNhanDatVe_BUS.GetGhiNhanDatVeList();
        BindingList<PhieuDatCho_DTO> listPhieuDat = PhieuDatCho_BUS.GetPhieuDatChoList();
        BindingList<ChuyenBay_DTO> listChuyenBay = ChuyenBay_BUS.GetChuyenBayList();
        List<TuyenBay_DTO> listTuyenBay = TuyenBay_BUS.GetTuyenBayList();
        List<HangVe_DTO> listHangVe = HangVe_BUS.GetHangVeList();
        List<HanhKhach_DTO> listHanhKhach = HanhKhach_BUS.GetHanhKhachList();
        ThamSo_DTO thamso = ThamSo_BUS.GetThamSoDetails();
        void DanhSachCombox()
        {
            for (int i = 0; i < listChuyenBay.Count; i++)
            {
                cmbChuyenBay.Properties.Items.Add(listChuyenBay[i].MaChuyenBay.ToString());
            }

            for (int i = 0; i < listTuyenBay.Count; i++)
            {
                cmbTuyenBay.Properties.Items.Add(listTuyenBay[i].MaTuyenBay.ToString());
            }

            cmbHangVe.ValueMember = HangVeColumns.MaHangVe;
            cmbHangVe.DisplayMember = HangVeColumns.TenHangVe;
            cmbHangVe.DataSource = listHangVe;

            HangVe.ValueMember = HangVeColumns.MaHangVe;
            HangVe.DisplayMember = HangVeColumns.TenHangVe;
            HangVe.DataSource = listHangVe;

        }
        void TinhTrangVe(string ChuyenBay, string HangVe)
        {
            TinhTrangVe_DTO tinhtrangveDTO = TinhTrangVe_BUS.GetTinhTrangVeDetails(ChuyenBay, HangVe);
            txtSoGheTrong.Text = tinhtrangveDTO.SoGheTrong.ToString();
            txtSoGheDat.Text = tinhtrangveDTO.SoGheDat.ToString();
            lbTinhTrangVe.Text = tinhtrangveDTO.MaTinhTrangVe;
        }
        void DonGiaDetails(string MaTuyenBay, string MaHangVe)
        {
            DonGiaVe_DTO dongiaDTO = DonGiaVe_BUS.GetDonGiaDetails(MaTuyenBay, MaHangVe);
            txtDonGia.Text = dongiaDTO.DonGia.ToString();
        }
        void ChuyenBayDetails(string ChuyenBay)
        {
            ChuyenBay_DTO chuyenbayDTO = ChuyenBay_BUS.GetChuyenBayDetails(ChuyenBay);
            dateNgay_Gio.Value = (DateTime)chuyenbayDTO.Ngay_Gio;
            txtThoiGianBay.Text = chuyenbayDTO.ThoiGianBay.ToString();
            cmbTuyenBay.Text = chuyenbayDTO.MaTuyenBay;

            TuyenBay_DTO tuyenbayDTO = TuyenBay_BUS.GetTuyenBayDetails(cmbTuyenBay.Text);
            string SanBayDi = tuyenbayDTO.SanBayDi;
            string SanBayDen = tuyenbayDTO.SanBayDen;

            SanBay_DTO sanbayDTO = SanBay_BUS.GetSanBayDetails(SanBayDi);
            txtSanBayDi.Text = sanbayDTO.TenSanBay.ToString();
            sanbayDTO = SanBay_BUS.GetSanBayDetails(SanBayDen);
            txtSanBayDen.Text = sanbayDTO.TenSanBay.ToString();

            string MaHangVe = cmbHangVe.SelectedValue.ToString();
            TinhTrangVe(ChuyenBay, MaHangVe);
            DonGiaDetails(cmbTuyenBay.Text, MaHangVe);

        }

        private void GhiNhanDatVe_GUI_Load(object sender, EventArgs e)
        {
            gridC_DSPhieuDat.DataSource = listGhiNhanDatVe;
            DanhSachCombox();
        }

        private void advB_DSPhieuDat_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                cmbChuyenBay.Enabled = false;
                btnDeleteVe.Enabled = true;

                GhiNhanDatVe_DTO row = (GhiNhanDatVe_DTO)advB_DSPhieuDat.GetFocusedRow();
                cmbChuyenBay.Text = row.ChuyenBay;
                dateNgay_Dat.Value = row.NgayDat;
                ChuyenBayDetails(row.ChuyenBay.ToString());

                cmbHangVe.SelectedValue = row.MaHangVe.ToString();
                txtThanhTien.Text = row.GiaTien.ToString();

                txtMaHanhKhach.Text = row.MaHanhKhach.ToString();
                txtTenHanhKhach.Text = row.TenHanhKhach;
                txtCMND.Text = row.CMND;
                txtDienThoai.Text = row.DienThoai;
            }
        }
        private void cmbChuyenBay_SelectedIndexChanged(object sender, EventArgs e)
        {
            string MaChuyenBay = cmbChuyenBay.Text;
            ChuyenBayDetails(MaChuyenBay);
        }
        private void cmbHangVe_SelectedIndexChanged(object sender, EventArgs e)
        {
            string MaTuyenBay = cmbTuyenBay.Text;
            string MaHangVe = cmbHangVe.SelectedValue.ToString();
            string MaChuyenBay = cmbChuyenBay.Text;

            DonGiaDetails(MaTuyenBay, MaHangVe);
            TinhTrangVe(MaChuyenBay, MaHangVe);
        }

        string CreateMaPhieu()
        {
            string Kq = "PD0";
            char[] c = "PD0".ToCharArray();
            if (listPhieuDat.Count == 0)
            {
                Kq = "PD01";
            }
            else
            {
                string Ma = PhieuDatCho_BUS.MaxMaPhieu();
                Ma = Ma.TrimStart(c);
                Kq += Convert.ToString(Convert.ToInt32(Ma) + 1);
            }
            return Kq;
        }
        string CreateMaHanhKhach()
        {
            string Kq = "HK0";
            char[] c = "HK0".ToCharArray();
            if (listHanhKhach.Count == 0)
            {
                Kq = "HK001";
            }
            else
            {
                string Ma = HanhKhach_BUS.MaxMaHanhKhach();
                Ma = Ma.TrimStart(c);
                Kq += Convert.ToString(Convert.ToInt32(Ma) + 1);
            }
            return Kq;
        }
        int KiemTraVe(string maChuyenBay, string maHangVe)
        {
            int flag = 0;
            TinhTrangVe_DTO tinhtrangveDTO = TinhTrangVe_BUS.GetTinhTrangVeDetails(maChuyenBay, maHangVe);
            if (tinhtrangveDTO.SoGheTrong != 0)
            {
                flag = 1;
            }
            return flag;
        }

        private void btnInsertVe_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                txtMaHanhKhach.Text = CreateMaHanhKhach();
                txtTenHanhKhach.Text = "";
                txtCMND.Text = "";
                txtDienThoai.Text = "";
                txtThanhTien.Text = "";
                dateNgay_Gio.Value = DateTime.Today;
                btnDeleteVe.Enabled = false;
                cmbChuyenBay.Enabled = true;

                btnInsertVe.Text = "Ghi Nhận";
                flag = 1;
            }
            else
            {
                if (txtTenHanhKhach.Text != "" && txtCMND.Text != "" && txtDienThoai.Text != "")
                {
                    if (cmbChuyenBay.Text != "")
                    {
                        if (KiemTraVe(cmbChuyenBay.Text, cmbHangVe.SelectedValue.ToString()) != 0)
                        {
                            advB_DSPhieuDat.AddNewRow();
                            GhiNhanDatVe_DTO row = (GhiNhanDatVe_DTO)advB_DSPhieuDat.GetFocusedRow();

                            HanhKhach_DTO hanhkhachDTO = new HanhKhach_DTO();
                            hanhkhachDTO.MaHanhKhach = txtMaHanhKhach.Text;
                            hanhkhachDTO.TenHanhKhach = row.TenHanhKhach;
                            hanhkhachDTO.CMND = row.CMND;
                            hanhkhachDTO.DienThoai = row.DienThoai;
                            HanhKhach_BUS.InsertHanhKhach(hanhkhachDTO);

                            PhieuDatCho_DTO phieudat = new PhieuDatCho_DTO();
                            phieudat.MaPhieuDat = CreateMaPhieu();
                            phieudat.MaChuyenBay = row.ChuyenBay;
                            phieudat.MaHangVe = row.MaHangVe;
                            phieudat.MaHanhKhach = txtMaHanhKhach.Text;
                            phieudat.NgayDat = row.NgayDat;
                            phieudat.GiaTien = float.Parse(txtDonGia.Text);
                            PhieuDatCho_BUS.InsertPhieuDatCho(phieudat);

                            TinhTrangVe_DTO tinhtrangveDTO = new TinhTrangVe_DTO();
                            tinhtrangveDTO.MaTinhTrangVe = lbTinhTrangVe.Text.ToString();
                            tinhtrangveDTO.MaChuyenBay = cmbChuyenBay.Text.ToString();
                            tinhtrangveDTO.MaHangVe = cmbHangVe.SelectedValue.ToString();
                            tinhtrangveDTO.SoGheTrong = int.Parse(txtSoGheTrong.Text) - 1;
                            tinhtrangveDTO.SoGheDat = int.Parse(txtSoGheDat.Text) + 1;
                            TinhTrangVe_BUS.UpdateTinhTrangVe(tinhtrangveDTO);

                            listGhiNhanDatVe = GhiNhanDatVe_BUS.GetGhiNhanDatVeList();
                            gridC_DSPhieuDat.DataSource = listGhiNhanDatVe;

                            btnDeleteVe.Enabled = true;
                            btnInsertVe.Text = "Lập Phiếu Đặt";
                            flag = 0;
                        }
                        else
                        {
                            MessageBox.Show("Hết Vé", "..:: Thông Báo ::..");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Chưa Chọn Chuyến Bay", "..:: Thông Báo ::..");
                    }
                }
                else
                {
                    MessageBox.Show("Thông Tin Hành khách Chưa Đủ", "..:: Thông Báo ::..");
                }
            }
        }
        private void btnDeleteVe_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Muốn Hủy Phiếu Đặt ?", "Thông Báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                GhiNhanDatVe_DTO row = (GhiNhanDatVe_DTO)advB_DSPhieuDat.GetFocusedRow();

                PhieuDatCho_DTO phieudatDTO = new PhieuDatCho_DTO();
                phieudatDTO.MaPhieuDat = row.MaPhieuDat;
                PhieuDatCho_BUS.DeletePhieuDatCho(phieudatDTO);

                TinhTrangVe_DTO tinhtrangDTO = TinhTrangVe_BUS.GetTinhTrangVeDetails(row.ChuyenBay, row.MaHangVe);
                tinhtrangDTO.MaTinhTrangVe = tinhtrangDTO.MaTinhTrangVe;
                tinhtrangDTO.MaChuyenBay = row.ChuyenBay;
                tinhtrangDTO.MaHangVe = row.MaHangVe;
                tinhtrangDTO.SoGheTrong = tinhtrangDTO.SoGheTrong + 1;
                tinhtrangDTO.SoGheDat = tinhtrangDTO.SoGheDat - 1;
                TinhTrangVe_BUS.UpdateTinhTrangVe(tinhtrangDTO);

                HanhKhach_DTO hanhkhachDTO = new HanhKhach_DTO();
                hanhkhachDTO.MaHanhKhach = row.MaHanhKhach;
                HanhKhach_BUS.DeleteHanhKhach(hanhkhachDTO);

                listGhiNhanDatVe = GhiNhanDatVe_BUS.GetGhiNhanDatVeList();
                gridC_DSPhieuDat.DataSource = listGhiNhanDatVe;
                MessageBox.Show("Bạn Đã Hủy Xong", "..:: Thông Báo ::..");

            }
        }

        private void advB_DSPhieuDat_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView view = sender as GridView;
            if (view.FocusedColumn.FieldName == "STT" || view.FocusedColumn.FieldName == "ChuyenBay" || view.FocusedColumn.FieldName == "MaHangVe" || view.FocusedColumn.FieldName == "GiaTien" || view.FocusedColumn.FieldName == "NgayDat")
                e.Cancel = true;
        }
        private void advB_DSPhieuDat_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            int STT = advB_DSPhieuDat.RowCount;
            advB_DSPhieuDat.SetRowCellValue(e.RowHandle, advB_DSPhieuDat.Columns[0], STT);
            advB_DSPhieuDat.SetRowCellValue(e.RowHandle, advB_DSPhieuDat.Columns["ChuyenBay"], cmbChuyenBay.Text);
            advB_DSPhieuDat.SetRowCellValue(e.RowHandle, advB_DSPhieuDat.Columns["MaHangVe"], cmbHangVe.SelectedValue);
            advB_DSPhieuDat.SetRowCellValue(e.RowHandle, advB_DSPhieuDat.Columns["GiaTien"], txtDonGia.Text);
            advB_DSPhieuDat.SetRowCellValue(e.RowHandle, advB_DSPhieuDat.Columns["TenHanhKhach"], txtTenHanhKhach.Text);
            advB_DSPhieuDat.SetRowCellValue(e.RowHandle, advB_DSPhieuDat.Columns["CMND"], txtCMND.Text);
            advB_DSPhieuDat.SetRowCellValue(e.RowHandle, advB_DSPhieuDat.Columns["DienThoai"], txtDienThoai.Text);
            advB_DSPhieuDat.SetRowCellValue(e.RowHandle, advB_DSPhieuDat.Columns["NgayDat"], DateTime.Today);
        }
        private void advB_DSPhieuDat_RowUpdated(object sender, RowObjectEventArgs e)
        {
            GhiNhanDatVe_DTO row = (GhiNhanDatVe_DTO)advB_DSPhieuDat.GetFocusedRow();

            HanhKhach_DTO hanhkhach = new HanhKhach_DTO();
            hanhkhach.MaHanhKhach = row.MaHanhKhach;
            hanhkhach.TenHanhKhach = row.TenHanhKhach;
            hanhkhach.CMND = row.CMND;
            hanhkhach.DienThoai = row.DienThoai;
            HanhKhach_BUS.UpdateHanhKhach(hanhkhach);
        }
        private void advB_DSPhieuDat_ValidateRow(object sender, ValidateRowEventArgs e)
        {
            ColumnView view = sender as ColumnView;
            GridColumn column3 = view.Columns["TenHanhKhach"];
            GridColumn column4 = view.Columns["CMND"];
            GridColumn column5 = view.Columns["DienThoai"];
            string txtHK = (string)view.GetRowCellValue(e.RowHandle, column3);
            string txtCMND = (string)view.GetRowCellValue(e.RowHandle, column4);
            string txtDienThoai = (string)view.GetRowCellValue(e.RowHandle, column5);
            if (txtHK == "") 
            {
                e.Valid = false;
                view.SetColumnError(column3, "Thông Tin Chưa Đúng");
            }
            if (txtCMND == "") 
            {
                e.Valid = false;
                view.SetColumnError(column4, "Thông Tin Chưa Đúng");
            }
            if (txtDienThoai == "")
            {
                e.Valid = false;
                view.SetColumnError(column5, "Thông Tin Chưa Đúng");
            }
        }
        private void advB_DSPhieuDat_InvalidRowException(object sender, InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.NoAction;
        }
    }
}
